<?php

/**
 * OTS support layer class
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 * @author Kofel <kofels@gmail.com>
 * @copyright 2008 (C) by Kofel
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
 * @todo 
 */
 
class OTSLayer
{
	private $config = array();
	private $sConfig = array();
	private $rConfig = array( 'sqlType' => 'sql_type', 'mysqlHost' => 'sql_host', 'mysqlUser' => 'sql_user', 'mysqlPass' => 'sql_pass', 'sqliteDatabse' => 'sql_db', 'mysqlDatabase' => 'sql_db' );
	
	public $OTSPath;
	
	public $type = 'dt';
	
	public $db;
	
	public function load()
	{
		//OTSPath load :)
		include 'config.php';
		$this->OTSPath = $OTSPath;
		if( !file_exists( $this->OTSPath . 'config.lua' ) )
		{
			Kernel::dropLayer('config');
			Kernel::dropLayer('session');
			Kernel::dropLayer('database');
			Kernel::layer('view')->load( true );
			if( $_GET['ajax'] != 'yes' )
			{
				Kernel::layer('controller')->load('install', 'index');
				Kernel::dropLayer('controller');
			}
			
			return false;
		}
		//Config parse :)

		foreach( file( $this->OTSPath . 'config.lua' ) as $line )
		{
			$match = array();
			if( preg_match( "/\s*([A-Za-z_-]+)\s*=\s*['\"\{]?([A-Za-z0-9\-_ \.,:\/\\\\]+)?[\}'\"]?/", $line, $match ) )
			{
				if( $this->rConfig[ $match[1] ] )
				{
					$this->config[ $this->rConfig[ $match[1] ] ] = $match[2];
					$this->type = 'tfs';
				}
				else
				{
					$this->config[ $match[1] ] = $match[2];
				}
			}
		}
		
		//if( $db = Kernel::layer('database') )
		//{
			require_once 'lib/POT/OTS.php';
		
			$Parameters = array('host'		=> $this->getConfig('sql_host'),
								'user'		=> $this->getConfig('sql_user'),
								'password'	=> $this->getConfig('sql_pass'),
								'database'	=> ( $this->getConfig('sql_type') == 'sqlite' ) 
								? $this->OTSPath . $this->getConfig('sql_db') : $this->getConfig('sql_db') );
			$Types = array('sqlite' => POT::DB_SQLITE, 'mysql' => POT::DB_MYSQL);
		
			POT::getInstance()->connect( $Types[ $this->getConfig('sql_type') ], $Parameters );
			
			$this->db = POT::getInstance()->getDBHandle();
			if( $db = Kernel::layer('database') )
			{
				$db->pdo = $this->db;
			}
		//}
		
	}
	
	public function getConfig( $name )
	{
		return $this->config[ $name ];
	}
	
	public function setConfig( $name, $value )
	{
		$this->sConfig[ $this->config[ $name ] ] = $value;
		return true;
	}
	
	public function path( $s = NULL )
	{
		if( $s )
		{
			$this->OTSPath = $s;
			return true;
		}
		
		return $this->OTSPath;
	}
	
	public function unload()
	{
		if( count( $this->sConfig ) > 0 )
		{
			$Content = file_get_contents( $this->OTSPath . 'config.lua' );
			
			foreach( $this->sConfig as $Old => $New )
			{
				str_replace( $Old, $New, $Content );
			}
			
			$h = fopen( $this->OTSPath . 'config.lua', 'w' );
			fwrite( $h, $Content );
			fclose( $h );
		}
	}
	
	public function password( $str )
	{
		switch( $this->getConfig('passwordtype') )
		{
			case 'sha1':
				return sha1( $str );
				break;
			case 'md5':
				return md5( $str );
				break;
			case 'plain':
				return $str;
				break;
			default:
				throw new KernelException('Why system can\'t verify your password hash type method?');
		}
	}
}
?>